//
//  SN_Personal_ViewController.m
//  Shiyun_Newspaper
//
//  Created by Sylar on 2017/6/5.
//  Copyright © 2017年 ShiYun. All rights reserved.
//

#import "SN_Personal_ViewController.h"

#import "SN_Setting_ViewController.h" //设置
#import "SN_QuestionCollect_Controller.h"

#import "SN_NewspaperColection_ViewController.h"
#import "SN_PersonalInfo_ViewController.h"
#import "SN_VIPCenterViewController.h"


@interface SN_Personal_ViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *titleArr;
    NSArray *iconArr;
    
}

@property (nonatomic, strong) UITableView *personalTable;
@property (nonatomic, strong) UIButton *messageBtn;

@property (nonatomic,strong) UIImageView *imv;

@end

@implementation SN_Personal_ViewController

- (instancetype)init {
    
    if (self == [super init]) {
        self.hidesBottomBarWhenPushed = NO;
    }
    
    return self;
}

- (void)loadView {
    
    [super loadView];
    
    self.navBar.navigationItem.title = @"我的";
    
    /*
    self.messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.messageBtn setFrame:CGRectMake(SCREENWIDTH - 44, 20, 44, 44)];
    [self.messageBtn setImage:[UIImage imageNamed:@"newMessage"] forState:(UIControlStateNormal)];
    [self.messageBtn addTarget:self action:@selector(messageAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.navBar addSubview:self.messageBtn];
     */
    
    titleArr = @[@"报纸收藏",@"习题收藏",@"会员中心",@"设置"];
    iconArr = @[@"paperCollection",@"questionCollection",@"vipCenter",@"settingIcon"];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];

    [self.personalTable reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.personalTable];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action

- (IBAction)messageAction:(id)sender {
    NSLog(@"新消息");
}


#pragma mark - UITableViewSourceData 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 || section == 2) {
        return 1;
    }else if (section == 1) {
        return 4;
    }
    return 0;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellID"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    if (section == 0) {
        UIImageView *headImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 45, 45)];
        headImg.layer.cornerRadius = 22.5;
        headImg.clipsToBounds = YES;
        NSString *logo = [SYAccountTool ShareAccount].account.logo;
        if (logo && ![logo hasPrefix:@"http"]) {
            logo = [REQUESTIMAGEURL stringByAppendingString:logo];
        }
        [headImg sd_setImageWithURL:[NSURL URLWithString:logo]];
        [cell.contentView addSubview:headImg];
        
        UIImageView *headBg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 10, 47, 45)];
        NSInteger isVip = [[SYAccountTool ShareAccount].account.isvip integerValue];
        [headBg setImage:[UIImage imageNamed:isVip == 1?@"personal_effective":@"personal_expired"]];
        [cell.contentView addSubview:headBg];
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(headImg.right+10, 25, 150, 17)];
        [nameLabel setFont:[UIFont systemFontOfSize:16]];
        [nameLabel setTextColor:colorFromHex(@"3b3b3b")];
        [nameLabel setText:[SYAccountTool ShareAccount].account.username];
        [cell.contentView addSubview:nameLabel];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    }else if (section == 1) {
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UIImageView *icon = [[UIImageView alloc]initWithFrame:CGRectMake(15, 13, 18, 18)];
        [icon setImage:[UIImage imageNamed:[iconArr objectAtIndex:row]]];
        [cell.contentView addSubview:icon];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(icon.right+5, 14, 100, 16)];
        [titleLabel setFont:[UIFont systemFontOfSize:16]];
        [titleLabel setTextColor:colorFromHex(@"3b3b3b")];
        titleLabel.text = [titleArr objectAtIndex:row];
        
        [cell.contentView addSubview:titleLabel];
        
        if (row < 3) {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(15, 43.5, SCREENWIDTH, 0.5)];
            [line setBackgroundColor:colorFromHex(@"dedede")];
            [cell addSubview:line];
        }
        
    }else if (section == 2) {
        
        UILabel *logoutLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 45)];
        logoutLabel.textAlignment = NSTextAlignmentCenter;
        logoutLabel.text = @"退出登录";
        [logoutLabel setTextColor:colorFromHex(@"fb524a")];
        logoutLabel.font = [UIFont systemFontOfSize:17];
        
        [cell.contentView addSubview:logoutLabel];
    }
    
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
    [footer setBackgroundColor:colorFromHex(@"f4f5f7")];
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 65;
    }else if (indexPath.section == 1){
        return 44;
    }else {
        return 45;
    }
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        SN_PersonalInfo_ViewController *vc = [[SN_PersonalInfo_ViewController alloc]init];
        
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            
            SN_NewspaperColection_ViewController *vc = [[SN_NewspaperColection_ViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 1) {
            
            SN_QuestionCollect_Controller *collectVC = [[SN_QuestionCollect_Controller alloc] init];
            [self.navigationController pushViewController:collectVC animated:YES];
            
        }else if (indexPath.row == 2) {
            
            SN_VIPCenterViewController *vc = [[SN_VIPCenterViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
        }else if (indexPath.row == 3) {
            
            SN_Setting_ViewController *settingVC = [[SN_Setting_ViewController alloc] init];
            [self.navigationController pushViewController:settingVC animated:YES];
        }
    }
    
    if (indexPath.section == 2) {
        NSMutableDictionary *param = [[NSMutableDictionary alloc]init];
        [param setObject:USERID forKey:@"userid"];
        [param setObject:@"api_user_logout" forKey:@"method"];
        
        [Utils getWithparameters:param showHUD:YES success:^(NSInteger recode, id jsonObject) {
            
            NSUserDefaults *udd=[NSUserDefaults standardUserDefaults];
            
            [udd removeObjectForKey:@"mobile"];
            [udd removeObjectForKey:@"token"];
            [udd removeObjectForKey:@"userid"];
            [udd removeObjectForKey:XZHEADURL];
            [udd removeObjectForKey:@"isLogin"];
            [udd synchronize];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
            
            
        } failure:^(NSInteger recode, NSError *error) {
            [self showHint:@"网络异常，请稍后重试！"];
        }];
        
    }
    
}

#pragma mark - Get

- (UITableView *)personalTable {
    
    if (_personalTable == nil) {
        _personalTable = [[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH - SafeAreaTopHeight) style:(UITableViewStylePlain)];
        _personalTable.delegate = self;
        _personalTable.dataSource = self;
        _personalTable.scrollEnabled = NO;
        _personalTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_personalTable setBackgroundColor:colorFromHex(@"f4f5f7")];
    }
    return _personalTable;
}

@end
